<template>
	<view class="container">
		<!-- 页面头部 -->
		<view class="page-header">
			<text class="page-title">积分登记</text>
			<view class="header-actions">
				<text class="add-btn" @click="openCreateDialog">新增</text>
			</view>
		</view>

		<!-- 搜索筛选 -->
		<view class="search-section">
			<view class="search-form">
				<view class="form-row">
					<view class="form-item">
						<text class="label">部门</text>
						<picker :value="departmentIndex" :range="departmentList" range-key="name" @change="onDepartmentChange">
							<view class="picker-view">
								<text class="picker-text">{{ selectedDepartment.name || '请选择部门' }}</text>
								<text class="picker-arrow">▼</text>
							</view>
						</picker>
					</view>
					<view class="form-item">
						<text class="label">积分类型</text>
						<picker :value="pointsTypeIndex" :range="pointsTypeList" range-key="label" @change="onPointsTypeChange">
							<view class="picker-view">
								<text class="picker-text">{{ selectedPointsType.label || '请选择类型' }}</text>
								<text class="picker-arrow">▼</text>
							</view>
						</picker>
					</view>
				</view>
				<view class="form-row">
					<view class="form-item">
						<text class="label">状态</text>
						<picker :value="statusIndex" :range="statusList" range-key="label" @change="onStatusChange">
							<view class="picker-view">
								<text class="picker-text">{{ selectedStatus.label || '请选择状态' }}</text>
								<text class="picker-arrow">▼</text>
							</view>
						</picker>
					</view>
					<view class="form-item">
						<text class="label">时间范围</text>
						<picker mode="date" :value="searchForm.startTime" @change="onStartTimeChange">
							<view class="picker-view">
								<text class="picker-text">{{ searchForm.startTime || '开始时间' }}</text>
								<text class="picker-arrow">▼</text>
							</view>
						</picker>
						<text class="time-separator">至</text>
						<picker mode="date" :value="searchForm.endTime" @change="onEndTimeChange">
							<view class="picker-view">
								<text class="picker-text">{{ searchForm.endTime || '结束时间' }}</text>
								<text class="picker-arrow">▼</text>
							</view>
						</picker>
					</view>
				</view>
				<view class="form-row">
					<view class="form-item">
						<text class="label">关键词</text>
						<input class="input" v-model="searchForm.keyword" placeholder="请输入事件描述或责任人" />
					</view>
				</view>
				<view class="search-buttons">
					<button class="btn btn-primary" @click="handleSearch">搜索</button>
					<button class="btn btn-secondary" @click="handleReset">重置</button>
				</view>
			</view>
		</view>

		<!-- 数据列表 -->
		<scroll-view class="list-scroll" scroll-y>
			<view class="list-container">
				<view
					v-for="(item, index) in tableData"
					:key="index"
					class="list-item"
				>
					<view class="item-header">
						<view class="record-info">
							<text class="record-time">{{ item.recordTime }}</text>
							<text class="department-name">{{ item.departmentName }}</text>
						</view>
						<view class="status-badge" :class="getStatusClass(item.status)">
							<text class="status-text">{{ getStatusText(item.status) }}</text>
						</view>
					</view>

					<view class="item-content">
						<view class="personnel-info">
							<text class="label">责任人：</text>
							<text class="value">{{ item.responsiblePersonName }}</text>
						</view>
						<view class="event-info">
							<text class="label">事件描述：</text>
							<text class="value">{{ item.eventDescription }}</text>
						</view>
						<view class="points-info">
							<text class="label">积分：</text>
							<text class="value" :class="item.pointsType === 'add' ? 'points-positive' : 'points-negative'">
								{{ item.pointsType === 'add' ? '+' : '-' }}{{ item.pointsValue }}
							</text>
						</view>
						<view class="recorder-info">
							<text class="label">记录人：</text>
							<text class="value">{{ item.recorderName }}</text>
						</view>
					</view>

					<view class="item-actions">
						<button class="btn btn-info" @click="viewDetail(item)">详情</button>
						<button
							v-if="item.status === 'pending'"
							class="btn btn-warning"
							@click="editRecord(item)"
						>
							编辑
						</button>
						<button
							v-if="item.status === 'pending'"
							class="btn btn-danger"
							@click="deleteRecord(item)"
						>
							删除
						</button>
					</view>
				</view>

				<view v-if="tableData.length === 0" class="empty-state">
					<text class="empty-text">暂无积分记录</text>
				</view>
			</view>
		</scroll-view>

		<!-- 新增/编辑对话框 -->
		<view v-if="showCreateDialog" class="create-popup-overlay" @click="closeCreatePopup">
			<view class="create-popup" @click.stop>
				<view class="popup-header">
					<text class="popup-title">{{ isEdit ? '编辑积分记录' : '新增积分记录' }}</text>
					<text class="close-btn" @click="closeCreatePopup">✕</text>
				</view>

				<scroll-view class="popup-scroll" scroll-y>
					<view class="popup-content">
						<view class="form-item">
							<text class="label">部门</text>
							<picker :value="formData.departmentIndex" :range="departmentList" range-key="name" @change="onFormDepartmentChange">
								<view class="picker-view">
									<text class="picker-text">{{ formData.selectedDepartment.name || '请选择部门' }}</text>
									<text class="picker-arrow">▼</text>
								</view>
							</picker>
						</view>

						<view class="form-item">
							<text class="label">责任人</text>
							<input class="input" v-model="formData.responsiblePersonName" placeholder="请输入责任人姓名" />
						</view>

						<view class="form-item">
							<text class="label">事件描述</text>
							<textarea class="textarea" v-model="formData.eventDescription" placeholder="请详细描述积分事件"></textarea>
						</view>

						<view class="form-row">
							<view class="form-item">
								<text class="label">积分类型</text>
								<view class="radio-group">
									<view class="radio-item" @click="formData.pointsType = 'add'">
										<view class="radio" :class="{ 'checked': formData.pointsType === 'add' }">
											<text class="radio-icon" v-if="formData.pointsType === 'add'">●</text>
										</view>
										<text class="radio-text">加分</text>
									</view>
									<view class="radio-item" @click="formData.pointsType = 'deduct'">
										<view class="radio" :class="{ 'checked': formData.pointsType === 'deduct' }">
											<text class="radio-icon" v-if="formData.pointsType === 'deduct'">●</text>
										</view>
										<text class="radio-text">扣分</text>
									</view>
								</view>
							</view>
							<view class="form-item">
								<text class="label">积分分值</text>
								<input class="input" type="number" v-model="formData.pointsValue" placeholder="请输入分值" />
							</view>
						</view>

						<view class="form-item">
							<text class="label">严重程度</text>
							<picker :value="formData.severityIndex" :range="severityList" range-key="label" @change="onSeverityChange">
								<view class="picker-view">
									<text class="picker-text">{{ selectedSeverity.label || '请选择严重程度' }}</text>
									<text class="picker-arrow">▼</text>
								</view>
							</picker>
						</view>

						<view class="form-item">
							<text class="label">地点</text>
							<input class="input" v-model="formData.location" placeholder="请输入事件发生地点" />
						</view>
					</view>
				</scroll-view>

				<view class="popup-actions">
					<button class="btn btn-secondary" @click="closeCreatePopup">取消</button>
					<button class="btn btn-primary" @click="submitForm" :disabled="submitting">
						<text v-if="!submitting">{{ isEdit ? '更新' : '创建' }}</text>
						<text v-else>提交中...</text>
					</button>
				</view>
			</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchForm: {
					departmentId: '',
					pointsType: '',
					status: '',
					startTime: '',
					endTime: '',
					keyword: ''
				},
				departmentIndex: 0,
				departmentList: [
					{ id: '1', name: '安全部' },
					{ id: '2', name: '生产部' },
					{ id: '3', name: '维修部' },
					{ id: '4', name: '工程部' },
					{ id: '5', name: '质量部' }
				],
				pointsTypeIndex: 0,
				pointsTypeList: [
					{ value: '', label: '全部类型' },
					{ value: 'add', label: '加分' },
					{ value: 'deduct', label: '扣分' }
				],
				statusIndex: 0,
				statusList: [
					{ value: '', label: '全部状态' },
					{ value: 'pending', label: '待审批' },
					{ value: 'approved', label: '已通过' },
					{ value: 'rejected', label: '已拒绝' },
					{ value: 'cancelled', label: '已取消' }
				],
				severityList: [
					{ value: 'low', label: '低' },
					{ value: 'medium', label: '中' },
					{ value: 'high', label: '高' }
				],
				tableData: [],
				formData: {
					departmentIndex: 0,
					selectedDepartment: {},
					responsiblePersonName: '',
					eventDescription: '',
					pointsType: 'add',
					pointsValue: 1,
					severityIndex: 0,
					location: ''
				},
				isEdit: false,
				currentRecord: null,
				submitting: false,
				showCreateDialog: false
			}
		},
		computed: {
			selectedDepartment() {
				return this.departmentList[this.departmentIndex] || {}
			},
			selectedPointsType() {
				return this.pointsTypeList[this.pointsTypeIndex] || {}
			},
			selectedStatus() {
				return this.statusList[this.statusIndex] || {}
			},
			selectedSeverity() {
				return this.severityList[this.formData.severityIndex] || {}
			}
		},
		onLoad() {
			this.loadData()
		},
		methods: {
			// 加载数据
			loadData() {
				// 模拟API调用
				const mockData = [
					{
						id: 1,
						recordTime: '2025-01-15 09:30',
						departmentName: '安全部',
						responsiblePersonName: '张三',
						eventDescription: '发现员工未佩戴安全帽进入施工现场',
						pointsType: 'deduct',
						pointsValue: 5,
						status: 'pending',
						recorderName: '李四',
						severity: 'medium',
						location: 'A区施工现场'
					},
					{
						id: 2,
						recordTime: '2025-01-15 14:20',
						departmentName: '生产部',
						responsiblePersonName: '赵六',
						eventDescription: '主动发现并报告安全隐患，避免事故发生',
						pointsType: 'add',
						pointsValue: 10,
						status: 'approved',
						recorderName: '孙七',
						severity: 'high',
						location: 'B车间'
					}
				]

				this.tableData = mockData
			},

			// 部门变化
			onDepartmentChange(e) {
				this.departmentIndex = e.detail.value
				this.searchForm.departmentId = this.selectedDepartment.id
			},

			// 积分类型变化
			onPointsTypeChange(e) {
				this.pointsTypeIndex = e.detail.value
				this.searchForm.pointsType = this.selectedPointsType.value
			},

			// 状态变化
			onStatusChange(e) {
				this.statusIndex = e.detail.value
				this.searchForm.status = this.selectedStatus.value
			},

			// 开始时间变化
			onStartTimeChange(e) {
				this.searchForm.startTime = e.detail.value
			},

			// 结束时间变化
			onEndTimeChange(e) {
				this.searchForm.endTime = e.detail.value
			},

			// 搜索
			handleSearch() {
				this.loadData()
			},

			// 重置
			handleReset() {
				this.searchForm = {
					departmentId: '',
					pointsType: '',
					status: '',
					startTime: '',
					endTime: '',
					keyword: ''
				}
				this.departmentIndex = 0
				this.pointsTypeIndex = 0
				this.statusIndex = 0
				this.handleSearch()
			},

			// 显示新增对话框
			openCreateDialog() {
				this.isEdit = false
				this.resetForm()
				this.showCreateDialog = true
			},

			// 编辑记录
			editRecord(item) {
				this.isEdit = true
				this.currentRecord = item
				this.formData = {
					departmentIndex: this.departmentList.findIndex(d => d.name === item.departmentName),
					selectedDepartment: this.departmentList.find(d => d.name === item.departmentName) || {},
					responsiblePersonName: item.responsiblePersonName,
					eventDescription: item.eventDescription,
					pointsType: item.pointsType,
					pointsValue: item.pointsValue,
					severityIndex: this.severityList.findIndex(s => s.value === item.severity),
					location: item.location
				}
				this.showCreateDialog = true
			},

			// 查看详情
			viewDetail(item) {
				uni.showToast({
					title: '查看详情功能开发中',
					icon: 'none'
				})
			},

			// 删除记录
			deleteRecord(item) {
				uni.showModal({
					title: '确认删除',
					content: '确定要删除这条积分记录吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							})
							this.loadData()
						}
					}
				})
			},

			// 表单部门变化
			onFormDepartmentChange(e) {
				this.formData.departmentIndex = e.detail.value
				this.formData.selectedDepartment = this.departmentList[e.detail.value]
			},

			// 严重程度变化
			onSeverityChange(e) {
				this.formData.severityIndex = e.detail.value
			},

			// 关闭新增对话框
			closeCreatePopup() {
				this.showCreateDialog = false
			},

			// 重置表单
			resetForm() {
				this.formData = {
					departmentIndex: 0,
					selectedDepartment: {},
					responsiblePersonName: '',
					eventDescription: '',
					pointsType: 'add',
					pointsValue: 1,
					severityIndex: 0,
					location: ''
				}
			},

			// 提交表单
			async submitForm() {
				if (!this.validateForm()) {
					return
				}

				this.submitting = true

				try {
					// 模拟API调用
					await new Promise(resolve => setTimeout(resolve, 1000))

					uni.showToast({
						title: this.isEdit ? '更新成功' : '创建成功',
						icon: 'success'
					})

					this.closeCreatePopup()
					this.loadData()

				} catch (error) {
					uni.showToast({
						title: this.isEdit ? '更新失败' : '创建失败',
						icon: 'none'
					})
				} finally {
					this.submitting = false
				}
			},

			// 表单验证
			validateForm() {
				if (!this.formData.selectedDepartment.id) {
					uni.showToast({
						title: '请选择部门',
						icon: 'none'
					})
					return false
				}

				if (!this.formData.responsiblePersonName.trim()) {
					uni.showToast({
						title: '请输入责任人姓名',
						icon: 'none'
					})
					return false
				}

				if (!this.formData.eventDescription.trim()) {
					uni.showToast({
						title: '请输入事件描述',
						icon: 'none'
					})
					return false
				}

				if (!this.formData.pointsValue || this.formData.pointsValue <= 0) {
					uni.showToast({
						title: '请输入有效的积分分值',
						icon: 'none'
					})
					return false
				}

				return true
			},

			// 获取状态样式类
			getStatusClass(status) {
				return `status-${status}`
			},

			// 获取状态文本
			getStatusText(status) {
				const statusMap = {
					pending: '待审批',
					approved: '已通过',
					rejected: '已拒绝',
					cancelled: '已取消'
				}
				return statusMap[status] || status
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		display: flex;
		flex-direction: column;
		background: #f5f5f5;
	}

	.page-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		background: #fff;
		border-bottom: 1rpx solid #e4e7ed;

		.page-title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
		}

		.header-actions {
			.add-btn {
				font-size: 28rpx;
				color: #409EFF;
			}
		}
	}

	.search-section {
		background: #fff;
		padding: 30rpx;
		margin-bottom: 20rpx;

		.search-form {
			.form-row {
				display: flex;
				margin-bottom: 30rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.form-item {
					flex: 1;
					margin-right: 30rpx;

					&:last-child {
						margin-right: 0;
					}

					.label {
						display: block;
						font-size: 28rpx;
						color: #333;
						margin-bottom: 15rpx;
						font-weight: 500;
					}

					.input {
						width: 100%;
						height: 80rpx;
						border: 2rpx solid #e4e7ed;
						border-radius: 8rpx;
						padding: 0 20rpx;
						font-size: 28rpx;
						background: #fff;
						box-sizing: border-box;
					}

					.picker-view {
						display: flex;
						align-items: center;
						justify-content: space-between;
						height: 80rpx;
						border: 2rpx solid #e4e7ed;
						border-radius: 8rpx;
						padding: 0 20rpx;
						background: #fff;

						.picker-text {
							font-size: 28rpx;
							color: #333;
						}

						.picker-arrow {
							font-size: 24rpx;
							color: #999;
						}
					}

					.time-separator {
						margin: 0 20rpx;
						font-size: 28rpx;
						color: #666;
					}
				}
			}

			.search-buttons {
				display: flex;
				gap: 20rpx;

				.btn {
					height: 80rpx;
					border-radius: 8rpx;
					font-size: 28rpx;
					border: none;

					&.btn-primary {
						background: #409EFF;
						color: #fff;
					}

					&.btn-secondary {
						background: #f0f0f0;
						color: #666;
					}
				}
			}
		}
	}

	.list-scroll {
		flex: 1;
	}

	.list-container {
		padding: 20rpx;

		.list-item {
			background: #fff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);

			.item-header {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 20rpx;

				.record-info {
					.record-time {
						display: block;
						font-size: 28rpx;
						font-weight: bold;
						color: #409EFF;
						margin-bottom: 5rpx;
					}

					.department-name {
						display: block;
						font-size: 24rpx;
						color: #666;
					}
				}

				.status-badge {
					padding: 8rpx 16rpx;
					border-radius: 12rpx;
					font-size: 24rpx;

					&.status-pending {
						background: #E6A23C;
						color: #fff;
					}

					&.status-approved {
						background: #67C23A;
						color: #fff;
					}

					&.status-rejected {
						background: #F56C6C;
						color: #fff;
					}

					&.status-cancelled {
						background: #909399;
						color: #fff;
					}
				}
			}

			.item-content {
				margin-bottom: 30rpx;

				.personnel-info,
				.event-info,
				.points-info,
				.recorder-info {
					display: flex;
					margin-bottom: 15rpx;

					&:last-child {
						margin-bottom: 0;
					}

					.label {
						font-size: 28rpx;
						color: #666;
						min-width: 160rpx;
					}

					.value {
						font-size: 28rpx;
						color: #333;
						flex: 1;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;

						&.points-positive {
							color: #67C23A;
							font-weight: bold;
						}

						&.points-negative {
							color: #F56C6C;
							font-weight: bold;
						}
					}
				}

				.event-info {
					.value {
						white-space: normal;
						word-break: break-all;
					}
				}
			}

			.item-actions {
				display: flex;
				gap: 15rpx;

				.btn {
					flex: 1;
					height: 70rpx;
					border-radius: 8rpx;
					font-size: 26rpx;
					border: none;

					&.btn-info {
						background: #409EFF;
						color: #fff;
					}

					&.btn-warning {
						background: #E6A23C;
						color: #fff;
					}

					&.btn-danger {
						background: #F56C6C;
						color: #fff;
					}
				}
			}
		}

		.empty-state {
			text-align: center;
			padding: 80rpx 0;

			.empty-text {
				font-size: 28rpx;
				color: #999;
			}
		}
	}

	.create-popup-overlay {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: flex-end;
		z-index: 1000;
	}

	.create-popup {
		background: #fff;
		border-radius: 20rpx 20rpx 0 0;
		max-height: 80vh;
		width: 100%;

		.popup-header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 30rpx;
			border-bottom: 1rpx solid #e4e7ed;

			.popup-title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
			}

			.close-btn {
				font-size: 36rpx;
				color: #999;
			}
		}

		.popup-scroll {
			max-height: 60vh;
		}

		.popup-content {
			padding: 30rpx;

			.form-item {
				margin-bottom: 30rpx;

				.label {
					display: block;
					font-size: 28rpx;
					color: #333;
					margin-bottom: 15rpx;
					font-weight: 500;
				}

				.input {
					width: 100%;
					height: 80rpx;
					border: 2rpx solid #e4e7ed;
					border-radius: 8rpx;
					padding: 0 20rpx;
					font-size: 28rpx;
					background: #fff;
					box-sizing: border-box;
				}

				.textarea {
					width: 100%;
					min-height: 120rpx;
					border: 2rpx solid #e4e7ed;
					border-radius: 8rpx;
					padding: 20rpx;
					font-size: 28rpx;
					background: #fff;
					box-sizing: border-box;
				}

				.picker-view {
					display: flex;
					align-items: center;
					justify-content: space-between;
					height: 80rpx;
					border: 2rpx solid #e4e7ed;
					border-radius: 8rpx;
					padding: 0 20rpx;
					background: #fff;

					.picker-text {
						font-size: 28rpx;
						color: #333;
					}

					.picker-arrow {
						font-size: 24rpx;
						color: #999;
					}
				}

				.form-row {
					display: flex;

					.form-item {
						flex: 1;
						margin-right: 30rpx;

						&:last-child {
							margin-right: 0;
						}
					}
				}

				.radio-group {
					display: flex;
					gap: 40rpx;

					.radio-item {
						display: flex;
						align-items: center;

						.radio {
							width: 40rpx;
							height: 40rpx;
							border: 2rpx solid #e4e7ed;
							border-radius: 50%;
							margin-right: 15rpx;
							display: flex;
							align-items: center;
							justify-content: center;

							&.checked {
								border-color: #409EFF;

								.radio-icon {
									color: #409EFF;
									font-size: 20rpx;
								}
							}
						}

						.radio-text {
							font-size: 28rpx;
							color: #333;
						}
					}
				}
			}
		}

		.popup-actions {
			display: flex;
			gap: 20rpx;
			padding: 30rpx;

			.btn {
				flex: 1;
				height: 80rpx;
				border-radius: 12rpx;
				font-size: 32rpx;
				font-weight: bold;
				border: none;

				&.btn-secondary {
					background: #f0f0f0;
					color: #666;
				}

				&.btn-primary {
					background: #409EFF;
					color: #fff;
				}

				&:disabled {
					opacity: 0.6;
				}
			}
		}
	}
</style>
